package cn.itheima.manage.controller;

import cn.itheima.manage.domain.Student;
import cn.itheima.manage.service.StudentService;

import java.util.Scanner;

public class StudnetController {
    private Scanner sc = new Scanner(System.in);
    private StudentService studentService = new StudentService();

    public void start() {
        lo:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choicce = sc.next();
            switch (choicce) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudentBySid();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("退出");
                    break lo;
                default:
                    System.out.println("请输入正确的选择");
                    break;
            }
        }
    }

    private void updateStudent() {
        Student[] students = studentService.findAllStudent();
        if (students == null) {
            System.out.println("查无信息,请添加后再试");
            return;
        }
        String sid;
        while (true) {
            System.out.println("请输入学生id:");
            sid = sc.next();
            boolean exists = studentService.isExists(sid);
            if (exists) {
                break;
            }else {
                System.out.println("您输入的学生id不存在,请重新输入!");
            }
        }
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        String age = sc.next();
        System.out.println("请输入学生生日:");
        String birthday = sc.next();
        Student stu = new Student();
        stu.setId(sid);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        studentService.updateStudent(sid,stu);
        System.out.println("修改成功");
    }

    public void deleteStudentBySid() {
        Student[] students = studentService.findAllStudent();
        if (students == null) {
            System.out.println("查无信息,请添加后再试");
            return;
        }
        String sid;
        while (true) {
            System.out.println("请输入学生id:");
            sid = sc.next();
            boolean exists = studentService.isExists(sid);
            if (exists) {
                break;
            }else {
                System.out.println("您输入的学生id不存在,请重新输入!");
            }
        }
        studentService.deleteStudentBySid(sid);
        System.out.println("删除成功");
    }

    public void findAllStudent() {
        Student[] students = studentService.findAllStudent();
        if (students == null) {
            System.out.println("查无信息,请添加后再试");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < students.length; i++) {
            Student stu = students[i];
            if (stu != null) {
                System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t\t" + stu.getBirthday());
            }
        }
    }

    public void addStudent() {
        String sid;
        while (true) {
            System.out.println("请输入学生id:");
            sid = sc.next();
            boolean exists = studentService.isExists(sid);
            if (exists) {
                System.out.println("您的学号已被占用,请重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        String age = sc.next();
        System.out.println("请输入学生生日:");
        String birthday = sc.next();
        Student stu = new Student();
        stu.setId(sid);
        stu.setName(name);
        stu.setAge(age);
        stu.setBirthday(birthday);
        boolean flag = studentService.addStudent(stu);
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }
}
